package com.dinner.util;

import com.dinner.model.MenuItem;
import com.dinner.model.MenuItemDao;
import com.dinner.model.Order;
import com.dinner.model.OrderDao;

public class OrderSubmissionCommand implements Command {
	public OrderSubmissionCommand(String consumerID, int menuID,
			String address, String phone, int serveNumber,
			String recounterName, MenuItemDao menuItemDao, OrderDao orderDao) {
		this.consumerID = consumerID;
		this.address = address;
		this.phone = phone;
		this.menuID = menuID;
		this.serveNumber = serveNumber;
		this.menuItemDao = menuItemDao;
		this.orderDao = orderDao;
		this.recounterName = recounterName;
	}

	@Override
	public void execute() {
		DinnerFactory factory = DinnerFactory.getFactory();
		Order order = factory.newOrder();
		order.setConsumerID(consumerID);
		order.setMenuItemName(menuItemDao.getMenuItemByID(menuID)
				.getMenuItemName());
		order.setSellerID(menuItemDao.getSellerByMenuID(menuID));
		order.setOrderAddress(address);
		order.setOrderPhone(phone);
		order.setServeCount(serveNumber);
		order.setOrderTime(TimeStampUtil.getCurrentTimeInStringFormat());
		order.setOrderState(Order.CONSUMER_ORDERED);
		MenuItem item = menuItemDao.getMenuItemByID(menuID);
		float price = item.getMenuItemPrice();
		recounter = factory.newPriceRecounter(recounterName);
		recounter.setPrice(price * serveNumber);
		order.setTotalPrice(recounter.getPrice());
		orderDao.saveOrder(order);
	}

	private IPriceRecounter recounter = null;
	private MenuItemDao menuItemDao = null;
	private OrderDao orderDao = null;
	private String consumerID = null;
	private String address = null;
	private String phone = null;
	private String recounterName = null;
	private int menuID = -1;
	private int serveNumber = -1;

}
